function selectOne(i) {
    let price = parseInt(document.getElementsByClassName("unitPrice")[i].innerHTML);
    let amount = parseInt(document.getElementsByClassName("amount")[i].value);
    let subtotal = document.getElementsByClassName("subtotal")[i];
    subtotal.innerHTML = (price * amount).toFixed(2);
    calOne();
    checkSelect();
}


function str(i) {
    let amount = document.getElementsByClassName("amount");
    var num = parseInt(amount[i].value)
    num--;
    if (num < 1) {
        num = 1;
    }
    amount[i].value = num;
    selectOne(i);
}

function add(i) {
    let amount = document.getElementsByClassName("amount");
    var num = parseInt(amount[i].value)
    num++;
    if (num < 0) {
        num = 0;
    }
    amount[i].value = num;
    selectOne(i);
    console.log(i);
}

function calOne() {
    let checkOne = document.getElementsByName("checkOne");
    let num = 0;
    for (let i = 0; i < checkOne.length; i++) {
        if (checkOne[i].checked) {
            let subtotal = document.getElementsByClassName("subtotal")[i];
            num += parseInt(subtotal.innerHTML);
        }
    }
    document.getElementById("total").innerHTML = num.toFixed(2);

}

function checkSelect() {
    let checkOne = document.getElementsByName("checkOne");
    let count = 0;
    for (let i = 0; i < checkOne.length; i++) {
        if (checkOne[i].checked) {
            count++;
        }
    }
    document.getElementById("btn_total").className = "";
    if (count !== 0) {
        document.getElementById("btn_total").className += "y_clear_button sucess";
        if (count === checkOne.length) {
            document.getElementById("checkAll").checked = true;
            document.getElementById("checkAll2").checked = true;
        }
        if (count !== checkOne.length) {
            document.getElementById("checkAll").checked = false;
            document.getElementById("checkAll2").checked = false;
        }
    } else {
        document.getElementById("btn_total").className += "y_clear_button failed";
    }




    document.getElementById("shop_count").innerHTML = count;
}

//全选按钮
function selectAll(i) {
    let ck;
    console.log(i);
    if (parseInt(i) === 1) {
        ck = document.getElementById("checkAll").checked;
    } else {
        ck = document.getElementById("checkAll2").checked;
    }
    document.getElementById("checkAll").checked = ck;
    document.getElementById("checkAll2").checked = ck;
    let checkOne = document.getElementsByName("checkOne");
    for (let i = 0; i < checkOne.length; i++) {
        checkOne[i].checked = ck;
        selectOne(i);
    }

}